package com.qidian.k12.service.impl;

import com.qidian.k12.pojo.Admin;
import com.qidian.k12.mapper.AdminMapper;
import com.qidian.k12.service.AdminService;
import com.qidian.k12.util.JsonTools;
import com.qidian.k12.util.K12Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * (Admin)表服务实现类
 *
 * @author makejava
 * @since 2021-10-28 09:02:09
 */
@Service("adminService")
public class AdminServiceImpl implements AdminService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private AdminMapper adminMapper;

    public K12Result checkLogin(String token){
        K12Result result = K12Result.notFond();
        //判断key是否存在
        String key = "user_"+token;
        if(redisTemplate.hasKey(key)){
            result = K12Result.ok();
            String json = redisTemplate.opsForValue().get(key);
            if(json!=null && !"".equals(json)) {
                Admin admin = JsonTools.string2Obj(json,Admin.class);
                result.setData(admin);
                // 更新存活时间
                redisTemplate.expire(key,30,TimeUnit.MINUTES);
                return result;
            }
        }
        return result;
    };
    public K12Result login(String adminName, String adminPass){
        K12Result result = K12Result.ok();
        //判断密码是否正确
        // 对传入的密码进行加密 (使用spring提供的工具产生MD5码)
        adminPass = DigestUtils.md5DigestAsHex(adminPass.getBytes());
        // 开始校验
        Admin admin = adminMapper.queryByName(adminName);
        if(admin == null && !admin.getAdminPass().equals(adminPass)){
            result.setStatus(K12Result.ResultStatus.ERROR);
            result.setMsg("用户名或密码错误");
            return result;
        }
        //登陆成功
        // 产生token  这个token不需要可读性，唯一即可，建议使用UUID。
        String token = UUID.randomUUID().toString().replace("-","");
        //将用户信息放入redis
        String key = "user_"+token;
        //删除admin的密码
        admin.setAdminPass(null);
        redisTemplate.opsForValue().set(key,JsonTools.obj2String(admin));
        redisTemplate.expire(key,30, TimeUnit.MINUTES);
        result.setMsg("登陆成功");
        Map map = new HashMap();

        map.put("admin",admin);
        map.put("token",token);
        result.setData(map);
        return result;
    }
    /**
     * 通过ID查询单条数据
     *
     * @param adminId 主键
     * @return 实例对象
     */
    @Override
    public Admin queryById(Long adminId) {
        return this.adminMapper.queryById(adminId);
    }


    /**
     * 新增数据
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public Admin insert(Admin admin) {
        this.adminMapper.insert(admin);
        return admin;
    }

    /**
     * 修改数据
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public Admin update(Admin admin) {
        this.adminMapper.update(admin);
        return this.queryById(admin.getAdminId());
    }

    /**
     * 通过主键删除数据
     *
     * @param adminId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long adminId) {
        return this.adminMapper.deleteById(adminId) > 0;
    }
}
